#!/bin/bash
#
# Example:
#
#   socat-forward-https-to-local-port news.ycombinator.com 8080
#
# Now you can open http://localhost:8080 in Chrome, and you should be
# browsing Hacker News.

url="${1}"
shift 1

if [ -z "${url}" ]
then
    1>&2 echo "usage:"
    1>&2 echo "  ${0} <url> [port=8080]"
    exit 1
fi

port="${1:-8080}"
shift 1

echo ""
echo "Try browsing to:"
echo ""
echo "  http://localhost:${port}"
echo ""
echo ""


# This doesn't work:
# exec socat "ssl:${url}:443" "TCP-LISTEN:${port},reuseaddr,fork"

# Spin loop instead.
while true
do
  for i in {0..1}
  do
    set -x
    socat "ssl:${url}:443" "TCP-LISTEN:${port},reuseaddr"
    set +x
  done
  sleep 0.1
done

